/* eslint-disable no-unused-expressions */
/**
 * 新版tb吊顶搜索、新建等模块初始化
 * 文档：https://thoughts.aone.alibaba-inc.com/workspaces/5cd4d712be825b5081e288ea/docs/5cd4d84ebe825b5081e28932
 */

import ReactDOM from 'react-dom';
import { navigate, navigateHref } from 'utils/router';
import { setLocale } from '@alife/yunxiao-common';
import initAboutOrganizationOrProject from './initAboutOrganizationOrProject';
import initCreateEntrance from './initCreateEntrance';
import initHelp from './initHelp';
import { ConfigProvider } from '@teamix/ui';
import './index.scss';
import registerInvite from './invite';
import exceed from 'utils/apimap';
import { getFeature } from 'utils/features';
import PersonalSettings from 'components/PersonalSettings';
// import { renderPage } from '@ali/homon-page-delivery';
// import SurveyElement from './question';
// import moment from 'moment';

export default function () {
  if (window.location.pathname.includes('/oauth')) {
    return;
  }
  if (!window.TB_NAVIGATION) {
    return;
  }

  window.TB_NAVIGATION.init({
    ...window.config,
    ...{
      logoType: 'logo',
      logoUrl:
        'https://img.alicdn.com/imgextra/i1/O1CN01sFFMSN1fK5qMMvG27_!!6000000003987-55-tps-102-102.svg',
      logoText: '代码管理',
      logoTextEN: 'Codeup',

      // 自定义吊顶个人设置点击事件
      personalSettingClick: getFeature('personal.settings.jumpToPage')
        ? undefined
        : () => {
            const idName = 'mountPersonalSettingEl';
            let mountPersonalSettingEl = document.getElementById(idName);
            if (!mountPersonalSettingEl) {
              mountPersonalSettingEl = document.createElement('div');
              mountPersonalSettingEl.id = idName;
              document.body.appendChild(mountPersonalSettingEl);
            }
            ReactDOM.render(
              <ConfigProvider prefix="yx-">
                <PersonalSettings
                  type="ssh"
                  defaultVisible
                  onClose={() => {
                    ReactDOM.unmountComponentAtNode(document.getElementById(idName));
                  }}
                />
              </ConfigProvider>,
              document.getElementById(idName),
            );
          },
    },
  });

  window.TB_NAVIGATION.ready(() => {
    window.TB_NAVIGATION.subscribeGoHomepage(() => {
      // 错误页不是单页面
      if (window.ERROR || window.location.pathname === '/search') {
        navigateHref('/');
      } else {
        navigate('/');
      }
    });

    // 设置使用中文
    const domainArr = document.domain.split('.');
    setLocale('zh_CN', true, { domain: `${domainArr[1]}.${domainArr[2]}` });
    // 国际化
    // window.TB_NAVIGATION.onChangeLang &&
    //   window.TB_NAVIGATION.onChangeLang((lang) => {
    //     const domainArr = document.domain.split('.');
    //     setLocale(lang, false, { domain: `${domainArr[1]}.${domainArr[2]}` });
    //   });

    // 邀请入口
    if (getFeature('nav.needInvite')) {
      setTimeout(() => {
        exceed
          .forceFetch({
            api: 'organization.info',
            cache: true,
          })
          .then(({ success, result = {} }) => {
            if (success && (result.role || {}).permissions.includes('organization.post.member')) {
              registerInvite();
            }
          });
      }, 1000);
    }

    const items = [];

    // 体验透镜暂时下架
    // if (moment().isAfter('2021/10/31 23:59:59')) {
    //   items.push({
    //     element: (
    //       <ConfigProvider prefix="yx-">
    //         <SurveyElement />
    //       </ConfigProvider>
    //     ),
    //     action: 'redirect',
    //     trigger: 'onClick',
    //     redirectUrl: 'https://survey.aliyun.com/apps/zhiliao/5m22NsAUb',
    //   });
    // }

    window.TB_NAVIGATION.CustomOperation && window.TB_NAVIGATION.CustomOperation.register(items);

    initAboutOrganizationOrProject();
    initCreateEntrance('useCacheIfExist');
    initHelp();
  });
}
